﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Maze_Generator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Maze Maze;
        private int[] Start;
        private int[] End;
        private int[] Position;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            Maze = new Maze(30, 20);
            Start = Maze.GetPosition();
            End = Maze.GetEnd();
            Position = Maze.GetStart();
            Maze.generate();
            DrawMaze(Maze, MazePanel);
        }

        /// <summary>
        /// Draws the maze.
        /// </summary>
        /// <param name="Maze"></param>
        private void DrawMaze(Maze Maze, WrapPanel MazePanel)
        {
            SolidColorBrush Black = new SolidColorBrush(Colors.Black);
            SolidColorBrush White = new SolidColorBrush(Colors.White);

            for (int y = 0; y < 20; y++)
            {
                for (int x = 0; x < 30; x++)
                {
                    if (Maze.GetCell(x, y))
                    {
                        DrawCell(x, y, White, MazePanel);
                    }
                    else
                    {
                        DrawCell(x, y, Black, MazePanel);
                    }
                }
            }
        }

        private void DrawCell(int x, int y, SolidColorBrush Color , WrapPanel MazePanel)
        {
            Rectangle Cell = new Rectangle();
            Cell.Width = 20;
            Cell.Height = 20;
            Cell.Fill = Color;
            MazePanel.Children.Add(Cell);
        }

        private void Main_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (Maze != null)
            {
                MazePanel.Children.Clear();
                DrawMaze(Maze, MazePanel);
            }

        }

        
    }
}
